﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class ViewRequisitionHistroy : System.Web.UI.Page
    {
        ViewRequisitionHistoryControl viewcont;
        StationeryInventoryEntities1 ent;
        string userName;
        //string userName = "COMM_Emp1";

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        userName = list[3];
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion


                viewcont = new ViewRequisitionHistoryControl();
                ent = new StationeryInventoryEntities1();
                int roleID = Convert.ToInt32(viewcont.checkingHeadOrNot(userName));

                if (!IsPostBack)
                {
                    calFromDate.Visible = false;
                    calToDate.Visible = false;
                    dgvReqHistoryViewDetail.Visible = false;
                    if (roleID == 4)
                    {
                        ddlEmployee.DataSource = viewcont.getEmployeeListByDepID(userName);
                        ddlEmployee.DataTextField = "EmployeeName";
                        ddlEmployee.DataValueField = "EmployeeID";
                        ddlEmployee.DataBind();
                        ddlEmployee.Items.Insert(0, "All");
                        dgvReqHistory.Columns[5].Visible = false;
                    }
                    else
                    {
                        //ddlEmployee.Enabled = false;
                        ddlEmployee.Visible = false;
                        lblEmpName.Visible = false;
                        dgvReqHistory.Columns[5].Visible = true;
                    }

                    Binding();
                }
            }
            catch (Exception ex)
            {
            }

            
        }

        public void Binding()
        {
            dgvReqHistory.DataSource = viewcont.getAllRequisitionByDept(userName);
            dgvReqHistory.DataBind();
        }

        public void DetailBinding(int i) {
            dgvReqHistoryViewDetail.DataSource = viewcont.getReqDetailOneByReqID(i);
            dgvReqHistoryViewDetail.DataBind();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (calFromDate.Visible == false)
            {
                calFromDate.Visible = true;
            }
            else {
                calFromDate.Visible = false;
            }
        }

        protected void CalFromDate_SelectionChanged(object sender, EventArgs e)
        {
            txtFromDate.Text = calFromDate.SelectedDate.ToShortDateString();
            calFromDate.Visible = false;
        }

        protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {
            if (calToDate.Visible == false)
            {
                calToDate.Visible = true;
            }
            else
            {
                calToDate.Visible = false;
            }
        }

        protected void calToDate_SelectionChanged(object sender, EventArgs e)
        {
            txtToDate.Text = calToDate.SelectedDate.ToShortDateString();
            calToDate.Visible = false;
        }

        protected void lbtnViewDetail_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int id = int.Parse(e.CommandArgument.ToString());
                dgvReqHistoryViewDetail.Visible = true;
                DetailBinding(id);
            }
            catch (Exception ex)
            {
            }

           
        }

        protected void lbtnDelete_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int id = int.Parse(e.CommandArgument.ToString());
                viewcont.deleteOneRequisition(id);
                Binding();
            }
            catch (Exception ex)
            {
            }

       
        }

        protected void dgvReqHistory_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                for (int i = 0; i < dgvReqHistory.Rows.Count + 2; i++)
                {
                    if (e.Row.RowType == DataControlRowType.DataRow)
                    {
                        e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#f6f6f6'");
                        e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                        LinkButton lbtnDelete = (LinkButton)e.Row.FindControl("lbtnDelete");
                        int cid = Convert.ToInt32(lbtnDelete.CommandArgument.ToString());
                        bool flat = viewcont.getRequisitionByStatus(cid); //true is pending, false is approve

                        if (flat == true)
                        {
                            lbtnDelete.Visible = true;

                        }
                        else
                        {
                            lbtnDelete.Visible = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }


            

            
           
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime fromDate = calFromDate.SelectedDate;
                DateTime toDate = calToDate.SelectedDate;

                int roleID = Convert.ToInt32(viewcont.checkingHeadOrNot(userName));

                dgvReqHistoryViewDetail.Visible = false;

                if (roleID == 4) //if head
                {
                    if (ddlEmployee.SelectedValue == "All")
                    {
                        if (fromDate != DateTime.MinValue && toDate != DateTime.MinValue)  //if data is not null
                        {
                            dgvReqHistory.DataSource = viewcont.getAllReqByDate(userName, fromDate, toDate);
                            dgvReqHistory.DataBind();
                        }
                        else //if date are null
                        {
                            Binding();
                        }
                    }
                    else
                    {
                        if (fromDate != DateTime.MinValue && toDate != DateTime.MinValue)
                        {
                            int eid = int.Parse(ddlEmployee.SelectedValue.ToString());
                            dgvReqHistory.DataSource = viewcont.getAllRequisitionByEmpId(eid, fromDate, toDate);
                            dgvReqHistory.DataBind();
                        }
                        else
                        {
                            int eid = int.Parse(ddlEmployee.SelectedValue.ToString());
                            dgvReqHistory.DataSource = viewcont.getIndividualReqByHead(eid);
                            dgvReqHistory.DataBind();
                        }
                    }

                }
                else
                {
                    dgvReqHistory.DataSource = viewcont.getAllReqByIndividualEmpWithDate(userName, fromDate, toDate);
                    dgvReqHistory.DataBind();
                    //bool flat = true;


                }
            }
            catch (Exception ex)
            {
            }

            
        }
    }
}

